﻿using lauimini_webform.BLL;
using lauimini_webform.Data;
using mydesign.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace lauimini_webform
{
    public partial class mine : System.Web.UI.Page
    {
        private string uploadDir;
        protected void Page_Load(object sender, EventArgs e)
        {
            uploadDir = Path.Combine(Request.PhysicalApplicationPath, "Uploads");
            if (Session["userAccount"] == null)
            {
                Response.Redirect("~/login.aspx");
            }
            MyDBContext dB = new MyDBContext();
            UserService userService = new UserService();
            UserTargetService userTargetService = new UserTargetService();
            string userAccount = Session["userAccount"].ToString();
            int userid = userService.getUserid(userAccount);
            List<MyComplish> myComplishes = dB.MyComplish.Where(x => x.usrid == userid).ToList();
            myComplish.DataSource = myComplishes;
            myComplish.DataBind();

        }

        protected void submitUser_Click(object sender, EventArgs e)
        {
            MyDBContext dB = new MyDBContext();
            UserService userService = new UserService();
            UserTargetService userTargetService = new UserTargetService();
            string userAccount = Session["userAccount"].ToString();
            Models.User user = userService.GetUser(userAccount);
            if (!oldPassword.Text.Trim().Equals(user.password.Trim()))
            {
                Response.Write("<script>alert('旧密码错误')</script>");
                return;
            }
            else if (!password.Text.Equals(repassword.Text))
            {
                Response.Write("<script>alert('两次输入的密码不一致')</script>");
                return;
            }
            else
            {
                var mysuer = dB.users.FirstOrDefault(x => x.id == user.id);
                mysuer.userName = userName.Text;
                mysuer.password = password.Text;
                dB.SaveChanges();
                Response.Write("<script>alert('修改成功');location.href='login.aspx'</script>");
            }
        }

        protected void submitHead_Click(object sender, EventArgs e)
        {
            if (FileUpload1.PostedFile.ContentLength > 204800)
            {
                Response.Write("<script>alert('文件大小不能超过200k');</script>");
                return;
            }
            string extension = Path.GetExtension(FileUpload1.PostedFile.FileName);
            switch (extension.ToLower())
            {
                case ".bmg":
                case ".png":
                case ".jpg":
                case ".gif":
                    break;
                default:
                    Response.Write("<script>alert('扩展名必须是：jpg，gif， bmg ，png');</script>");
                    return;

            }
            try
            {
                string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
                string fullPath = Path.Combine(uploadDir, Guid.NewGuid().ToString() + Path.GetExtension(fileName));

                MyDBContext dB = new MyDBContext();
                UserService userService = new UserService();
                UserTargetService userTargetService = new UserTargetService();
                string userAccount = Session["userAccount"].ToString();
                int userid = userService.getUserid(userAccount);
                var mysuer = dB.users.FirstOrDefault(x => x.id == userid);

                FileUpload1.SaveAs(fullPath);

                string relative = urlconvertor(fullPath);

                mysuer.headImg = relative;
                dB.SaveChanges();
                Response.Write("<script>alert('上传成功');</script>");
            }
            catch (Exception)
            {

                throw;
            }


        }

        private string urlconvertor(string imagesurl1)
        {
            string tmpRootDir = Server.MapPath(System.Web.HttpContext.Current.Request.ApplicationPath.ToString());//获取程序根目录
            string imagesurl2 = imagesurl1.Replace(tmpRootDir, ""); //转换成相对路径
            imagesurl2 = imagesurl2.Replace(@"/", @"/");
            //imagesurl2 = imagesurl2.Replace(@"Aspx_Uc/", @"");
            return imagesurl2;
        }

        protected void downloadHead_Click(object sender, EventArgs e)
        {
            MyDBContext dB = new MyDBContext();
            UserService userService = new UserService();
            UserTargetService userTargetService = new UserTargetService();
            string userAccount = Session["userAccount"].ToString();
            Models.User user = userService.GetUser(userAccount);
        }



    }
}